import {
  selectMeasuringToolsCount as selectMeasuringToolsCountApi,
  selectMeasuringToolsWorkingCount as selectMeasuringToolsWorkingCountApi,
  selectSimpleMeasuringTools as selectSimpleMeasuringToolsApi,
  loadIndicatorActualTimePage as loadIndicatorActualTimePageApi,
  loadIndicatorActualTimeByToolsId as loadIndicatorActualTimeByToolsIdApi,
} from '@/api/modules/measuringTools'

export function useElectricQualityAnalysis() {
  const cardData = ref({
    zdCount: 0,
    toolsCount: 0,
    indicatorCount: 0,
  })

  const pieOriginData = ref({})
  const equipmentData = ref({})
  const pointValData = shallowRef<any>([])
  const pagination = reactive({
    total: 0,
    pageSize: 5,
    pageNumber: 1,
  })
  const linesLoading = ref(true)

  const linesOriginData = shallowRef<{
    name: string
    series: { name: string, data: any[], unit: string }[]
    xaxis: {
      data: string[]
    }
  }>({
    name: '',
    series: [],
    xaxis: {
      data: [],
    },
  })

  async function selectMeasuringToolsCount() {
    const { data } = await selectMeasuringToolsCountApi()
    cardData.value = data
  }

  async function selectMeasuringToolsWorkingCount() {
    const { data } = await selectMeasuringToolsWorkingCountApi()
    pieOriginData.value = data
  }

  async function selectSimpleMeasuringTools() {
    const { data } = await selectSimpleMeasuringToolsApi()
    equipmentData.value = data
  }

  async function loadIndicatorActualTimePage() {
    const { pageNumber, pageSize } = pagination
    const { data } = await loadIndicatorActualTimePageApi(pageNumber, pageSize)
    pointValData.value = data.records || []
    pagination.total = data.total
  }

  async function loadIndicatorActualTimeByToolsId(toolsId: number) {
    linesLoading.value = true
    try {
      const { data } = await loadIndicatorActualTimeByToolsIdApi(toolsId)
      linesOriginData.value = data.lineChartVo || {}
    }
    finally {
      linesLoading.value = false
    }
  }

  return {
    /** 卡片数据 */
    cardData,
    /** 设备运行状态数据 */
    pieOriginData,
    /** 计量器具下拉数据 */
    equipmentData,
    /** 测点当前值数据 */
    pointValData,
    /** 趋势图数据 */
    linesOriginData,
    linesLoading,

    pagination,

    /** 加载卡片数据 */
    selectMeasuringToolsCount,
    /** 加载设备运行状态数据 */
    selectMeasuringToolsWorkingCount,
    /** 加载计量器具下拉数据 */
    selectSimpleMeasuringTools,
    /** 加载测点当前值数据 */
    loadIndicatorActualTimePage,
    /** 加载趋势图数据 */
    loadIndicatorActualTimeByToolsId,
  }
}
